<?php

namespace backend\models;

use Yii;
use backend\models\SysItem;
use backend\models\Image;
use yii\helpers\ArrayHelper;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "taozhuang_pos".
 *
 * @property integer $id
 * @property integer $tzid
 * @property string $show_name
 * @property string $fab_name
 * @property string $mat_name
 * @property integer $img_id
 * @property integer $order
 * @property integer $pos
 * @property integer $father
 * @property integer $x
 * @property integer $y
 * @property string $anchor_x
 * @property string $anchor_y
 * @property string $rotate
 * @property string $zoom
 * @property integer $mirror
 * @property integer $item_id
 * @property integer $pay_state
 * @property integer $count
 */
class TaozhuangPos extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'taozhuang_pos';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['tzid', 'img_id', 'order', 'pos', 'father', 'x', 'y', 'mirror', 'item_id', 'pay_state', 'count'], 'integer'],
            [['anchor_x', 'anchor_y', 'rotate', 'zoom'], 'number'],
            [['show_name', 'fab_name', 'mat_name'], 'string', 'max' => 32],
            [['tzid', 'pos', 'order'], 'unique', 'targetAttribute' => ['tzid', 'pos', 'order'], 'message' => 'The combination of Tzid, Order and Pos has already been taken.']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'tzid' => 'Tzid',
            'show_name' => 'Show Name',
            'fab_name' => 'Fab Name',
            'mat_name' => 'Mat Name',
            'img_id' => 'Img ID',
            'order' => 'Order',
            'pos' => 'Pos',
            'father' => 'Father',
            'x' => 'X',
            'y' => 'Y',
            'anchor_x' => 'Anchor X',
            'anchor_y' => 'Anchor Y',
            'rotate' => 'Rotate',
            'zoom' => 'Zoom',
            'mirror' => 'Mirror',
            'item_id' => 'Item ID',
            'pay_state' => 'Pay State',
            'count' => 'Count',
        ];
    }
    
    public function getSysItem()
    {
        return $this->hasOne(SysItem::className(), ['id'=>'item_id']);
    }
    public function getDefaultImg()
    {
        return $this->hasOne(Image::className(), ['id'=>'img_id']);
    }
    public function getFitClass()
    {
        return $this->hasOne(DiyClassGrp::className(), ['class_id'=>'class_id'])->viaTable(SysItem::tableName(),['id'=>'item_id']);
    }
    public function getFitSpace()
    {
        $sql="SELECT sys_item_space.space_id FROM  sys_item_space WHERE sys_item_space.item_id=".$this->item_id;
        return ArrayHelper::getColumn(SysItemSpace::findBySql($sql)->asArray()->all(), 'space_id');
    }
    public static function getItemFbmt($id)
    {
        $sql="SELECT base_sys_preview.fid,base_sys_preview.mid,
            base_sys_fabric.`name` AS fab_name,
            base_sys_material.`name` AS mat_name,
            img2.url AS fbmt_preview, 
            img3.url AS fab_url, 
            img4.url AS mat_url
            FROM taozhuang_pos 
            LEFT JOIN base_sys_preview ON taozhuang_pos.item_id=base_sys_preview.item_id 
            LEFT JOIN base_sys_fabric ON base_sys_fabric.id=base_sys_preview.fid 
            LEFT JOIN base_sys_material ON base_sys_material.id=base_sys_preview.mid 
            LEFT JOIN sys_image img2 ON img2.id=base_sys_preview.img_tz_id
            LEFT JOIN sys_image img3 ON base_sys_fabric.img_small_id=img3.id 
            LEFT JOIN sys_image img4 ON base_sys_material.img_small_id=img4.id 
            WHERE taozhuang_pos.id=".$id;
        return ActiveRecord::findBySql($sql)->asArray()->all();
    }
    /**
     * 获取单品信息
     */
    public function getSysitemInfo()
    {
        return $this->hasOne(SysItem::className(),['id'=>'item_id']);
    }
}
